class NativeBriage {

	public static init()
	{
		if(egret.Capabilities.runtimeType!=egret.RuntimeType.NATIVE)
        {
			LogManager.log("当前不支持Native功能");
            return;
        }
		egret.ExternalInterface.addCallback("reciveNative",NativeBriage.reciveNative);
		LogManager.log("open ")
		NativeBriage.getIMEI();
		NativeBriage.getPkgVersion();
		NativeBriage.enableBackKey(true);
		NativeBriage.stopSleep("true");

		// NativeBriage.iosBuy("game_zc_01");
	}
	/**获取版本号 */
	public static getPkgVersion(){
		NativeBriage.callNativeFun("getVer");
	}
	/**	 * 获取IMEI	 */
	public static getIMEI()	{
		NativeBriage.callNativeFun("getIMEI");
	}
	/**	 * 获取剪切板	 */
	public static getClipBoard()	{
		NativeBriage.callNativeFun("getClipBoard");
	}
	/**设置剪切板 */
	public static setClipBoard(msg:string)	{
		NativeBriage.callNativeFun("setClipBoard",msg);
	}
	/**打开浏览器 */
	public static openSystemWeb(url:string)	{
		if(egret.Capabilities.runtimeType==egret.RuntimeType.WEB)
			window.open(url);
		else
			NativeBriage.callNativeFun("openSystemWeb",url);
	}
	/**打开应用商店 */
	public static openAppShop(id:string){
		NativeBriage.callNativeFun("openAppShop",id);
	}
	/**阻止休眠 */
	public static stopSleep(value){
		NativeBriage.callNativeFun("stopSleep",value);
	}
	/** 屏蔽返回键 */
	public static enableBackKey(value){
		NativeBriage.callNativeFun("enableBackKey",value);
	}
	public static installApk(url){
		NativeBriage.callNativeFun("installApk",url);
	}
	/** 获取是否有网络 */
	public static isConnectionAvailable(){
		NativeBriage.callNativeFun("isConnectionAvailable");
	}
	/**
	 * 苹果内购
	 */
	public static iosBuy(product:string)
	{
		NativeBriage.callNativeFun("iosPay",product);
	}

	public static callNativeFun(fun:string,param:any={})
	{
		var data={method:fun,value:param};
		var str=JSON.stringify(data);
		LogManager.log("call native:"+str);
		egret.ExternalInterface.call("callNative",str);
	}

	public static reciveNative(msg:string)
	{
		var obj:any=JSON.parse(msg);
		LogManager.log("reciveNative:"+msg);
		switch(obj.key)
		{
			case "clipBoard":
				break;
			case "NetAvailabl":
				App.NetAvailable=obj.value=="true";
				break;
			case "IMEI":
				App.IMIE=obj.value;
				break;
			case "ver":
				App.pkgVersion=obj.value;
				break;
			case "iosPayResult":
				NativeBriage.onIosBuyResult(obj.product,obj.result,obj.receipt);
				break;
		}
	}
	/**
	 * @param product	产品ID 
	 * @param result	结果
	 * 						0成功
	 * 						1不允许程序内付费购买
	 * 						2empty product
	 * 						3购买失败，或取消购买
	 * 						4已经购买过了
	 * @param receipt	小票用于服务器验证
	 */
	private static onIosBuyResult(product:string,result:number,receipt:string)
	{
		NotificationManager.instance.sendMsg("IOSPayResult",[product,result,receipt]);
	}
}